package me.cxlr.qinlauncher2.viewmodel.drawer;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import me.cxlr.qinlauncher2.dao.AppDao;
import me.cxlr.qinlauncher2.dao.FolderDao;
import me.cxlr.qinlauncher2.model.App;
import me.cxlr.qinlauncher2.model.AppAndFolder;
import me.cxlr.qinlauncher2.model.Folder;
import me.cxlr.qinlauncher2.util.Han2PinYinUtil;
import me.cxlr.qinlauncher2.util.SharedPreferencesUtil;

/**
 * @author 酷安用户:山轮纸
 */
public class DrawerViewModel extends ViewModel {
    private final AppDao appDao;
    private final FolderDao folderDao;
    private final MutableLiveData<List<AppAndFolder>> appAndFolderList;

    public DrawerViewModel() {
        appDao = new AppDao();
        folderDao = new FolderDao();
        appAndFolderList = new MutableLiveData<>();
    }

    /**
     * 获取不在文件夹中的应用和文件夹
     *
     * @param sorting 排序
     * @return
     */
    public LiveData<List<AppAndFolder>> getAppAndFolderList(int sorting, int systemAppSorting) {
        List<AppAndFolder> list = new ArrayList<>(16);
        List<App> appList = appDao.findAppListByNotInFolder(sorting, systemAppSorting);
        List<Folder> folderList = folderDao.findAllFolder();

        // 有文件夹
        if (folderList.size() > 0) {
            String folderSorting = SharedPreferencesUtil.getInstance().getString("app_drawer_folder", "1");
            switch (folderSorting) {
                // 文件夹置顶
                case "1":
                    for (Folder folder : folderList) {
                        list.add(AppAndFolder.builder().isApp(false).isFolder(true).folder(folder).build());
                    }
                    if (appList.size() > 0) {
                        for (App app : appList) {
                            list.add(AppAndFolder.builder().isApp(true).isFolder(false).app(app).build());
                        }
                    }
                    break;
                // 文件夹置底
                case "2":
                    if (appList.size() > 0) {
                        for (App app : appList) {
                            list.add(AppAndFolder.builder().isApp(true).isFolder(false).app(app).build());
                        }
                    }
                    for (Folder folder : folderList) {
                        list.add(AppAndFolder.builder().isApp(false).isFolder(true).folder(folder).build());
                    }
                    break;
                default:
                    break;
            }
        }
        // 无文件夹
        else {
            if (appList.size() > 0) {
                for (App app : appList) {
                    list.add(AppAndFolder.builder().isApp(true).isFolder(false).app(app).build());
                }
            }
        }
        appAndFolderList.setValue(list);
        return appAndFolderList;
    }

    /**
     * 获取文件夹里的App
     *
     * @param folder 文件夹
     * @return 文件夹内App
     */
    public List<App> getAppInFolderList(Folder folder) {
        List<App> appList = appDao.findAppInFolder(folder);
        return appList;
    }

    /**
     * 获取文件夹
     *
     * @return 文件夹
     */
    public List<Folder> getFolderList() {
        return folderDao.findAllFolder();
    }

    /**
     * 创建文件夹
     *
     * @param folderName 文件夹名称
     */
    public void saveFolder(String folderName) {
        Folder folder = Folder.builder().id(UUID.randomUUID().toString().replaceAll("-", "")).folderName(folderName).build();
        folderDao.saveFolder(folder);
    }

    /**
     * 给文件夹起新名称
     *
     * @param folder   文件夹
     * @param newLabel 新名称
     */
    public void updateAppById(Folder folder, String newLabel) {
        folder.setFolderName(newLabel);
        folderDao.updateFolder(folder);
    }

    /**
     * 删除文件夹并且将APP移出
     *
     * @param folder 文件夹
     */
    public void deleteFolder(Folder folder) {
        List<App> appList = appDao.findAppInFolder(folder);
        if (appList != null) {
            for (App entity : appList) {
                entity.getAppDrawer().setInFolder(false);
                entity.getAppDrawer().setFolder(null);
                entity.getAppDrawer().setSortingInFolder(-1);
            }
            appDao.updateApp(appList);
        }
        folderDao.deleteFolder(folder);
    }

    /**
     * 将选中的app放入文件夹
     *
     * @param folder 选中的文件夹
     * @param app    要放入文件夹的App
     */
    public String chosenIntoFolder(Folder folder, App app) {
        String str = "完成";
        List<App> appList = appDao.findAppInFolder(folder);
        if (appList == null) {
            app.getAppDrawer().setInFolder(true);
            app.getAppDrawer().setFolder(folder);
            app.getAppDrawer().setSortingInFolder(0);
            appDao.updateApp(app);
        } else if (appList.size() == 9) {
            str = "文件夹已满";
        } else {
            app.getAppDrawer().setInFolder(true);
            app.getAppDrawer().setFolder(folder);
            app.getAppDrawer().setSortingInFolder(appList.size());
            appDao.updateApp(app);
        }
        return str;
    }

    /**
     * 文件夹里的应用上移或下移
     */
    public void appInFolderUpOrDown(String action, App app, Folder folder) {
        if ("up".equals(action)) {
            App app2 = appDao.findAppInFolderBySortingInFolder(folder, app.getAppDrawer().getSortingInFolder() - 1);
            int changeSorting;
            changeSorting = app2.getAppDrawer().getSortingInFolder();
            app2.getAppDrawer().setSortingInFolder(app.getAppDrawer().getSortingInFolder());
            app.getAppDrawer().setSortingInFolder(changeSorting);
            appDao.updateApp(app);
            appDao.updateApp(app2);
        } else if ("down".equals(action)) {
            App app2 = appDao.findAppInFolderBySortingInFolder(folder, app.getAppDrawer().getSortingInFolder() + 1);
            int changeSorting;
            changeSorting = app2.getAppDrawer().getSortingInFolder();
            app2.getAppDrawer().setSortingInFolder(app.getAppDrawer().getSortingInFolder());
            app.getAppDrawer().setSortingInFolder(changeSorting);
            appDao.updateApp(app);
            appDao.updateApp(app2);
        }
    }

    /**
     * 把文件夹里的App移出
     *
     * @param app 文件夹内的App
     */
    public void appInFolderOut(App app, Folder folder) {
        app.getAppDrawer().setInFolder(false);
        app.getAppDrawer().setFolder(null);
        app.getAppDrawer().setSortingInFolder(-1);
        appDao.updateApp(app);

        List<App> appInFolderList = appDao.findAppInFolder(folder);
        for (int i = 0; i < appInFolderList.size(); i++) {
            appInFolderList.get(i).getAppDrawer().setSortingInFolder(i);
        }
        appDao.updateApp(appInFolderList);
    }

    /**
     * 隐藏应用
     *
     * @param app App
     */
    public void hiddenApp(App app) {
        if (app != null) {
            app.getAppDrawer().setHidden(true);
            appDao.updateApp(app);
        }
    }
}
